package com.hemerasolutions.v2trainer.component.list;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.mutable.MutableInt;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.list.VideosListCompInterface;
import com.hemerasolutions.v2trainer.model.multimedia.RemoteVideo;
import com.hemerasolutions.v2trainer.model.multimedia.Video;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.EquipmentServiceInterface;
import com.hemerasolutions.v2trainer.service.interfaces.ModalityServiceInterface;
import com.hemerasolutions.v2trainer.service.interfaces.MultimediaServiceInterface;

@Controller
@Component("videosListComp")
@Scope("prototype")
public class VideosListComp extends BaseComponent implements VideosListCompInterface {

	private static final long serialVersionUID = 1L;

	private List<Video> videos;
	
	private Video selVideo;
	
	private MutableInt progress;
	
	@Autowired
	private MultimediaServiceInterface multimediaService;
	
	@Autowired
	private EquipmentServiceInterface equipmentService;

	@Autowired
	private ModalityServiceInterface modalityService;
	
	
	private int videoType = 1;
	
	public static final int EXERCISE_VIDEO = 1;

	public static final int EQUIPMENT_VIDEO = 2;
	
	
	
	public VideosListComp(){
		super();
	}

	@PostConstruct
	public void init(){
		super.init();
		this.videos = new ArrayList<Video>();
		this.refresh();
	}
	
	
	@Override
	public void addVideo(){
		Video video = null;
		if(videoType == EXERCISE_VIDEO){
			video = this.multimediaService.copyDefaultExerciseVideo();
			this.videos.add(videos.size(),video);
		}else if(videoType == EQUIPMENT_VIDEO){
			video = this.multimediaService.copyDefaultEquipmentVideo();
			this.videos.add(videos.size(),video);
		}
	}
	
	@Override
	public void deleteVideo(){
		this.videos.remove(this.selVideo);
	}
	
	@Override
	public void uploadToYouTube(){
	    if (this.selVideo.getRemoteVideo().getUploadedFile() != null)
	    {
			try {
				this.progress = new MutableInt(0);
				this.selVideo.setRemoteVideo(multimediaService.uploadToYouTube(selVideo.getRemoteVideo(),this.progress));
				this.selVideo.setRemoteVideo(this.multimediaService.saveRemoteVideo(this.selVideo.getRemoteVideo()));
				this.showInfoMessage(this.localizedText("SavedWithSuccess"),this.localizedText("UploadedWithSuccess"));
				this.showInfoMessage(this.localizedText("Warning"),this.localizedText("VideoProcessingMayTakeAWhile"));
				
				

			} catch (ExceptionService e) {
				this.showErrorMessage(this.localizedText("Error"), e.getMessage());
			}
	    }
	}
	
	@Override
	public void upload(FileUploadEvent ev) {

		selVideo.setRemoteVideo(new RemoteVideo());
		selVideo.setName(	this.videoType == EQUIPMENT_VIDEO ? 
							this.localizedText("EquipmentVideo"):
							this.localizedText("ExerciseVideo"));
		
		selVideo.getRemoteVideo().setUploadedFile(ev.getFile());
	}	
	
	
	@Override
	public void refresh() {
		Video video = null;
		
		if(this.videos!= null && this.videos.isEmpty()){
			if(this.videoType == EXERCISE_VIDEO){
				video = this.multimediaService.copyDefaultExerciseVideo();
				videos.add(video);
			}else if(this.videoType == EQUIPMENT_VIDEO){
				video = this.multimediaService.copyDefaultEquipmentVideo();
				videos.add(video);
			}
		}
	}
		
	

	@Override
	public List<Video> getVideos() {
		return videos;
	}

	@Override
	public void setVideos(List<Video> videos) {
		this.videos = videos;
	}

	@Override
	public Video getSelVideo() {
		return selVideo;
	}

	@Override
	public void setSelVideo(Video selVideo) {
		this.selVideo = selVideo;
	}
	
	@Override
	public MutableInt getProgress() {
		return progress;
	}

	@Override
	public void setProgress(MutableInt progress) {
		this.progress = progress;
	}

	@Override
	public int getVideoType() {
		return videoType;
	}

	@Override
	public void setVideoType(int videoType) {
		this.videoType = videoType;
	}


	
}
